package com.whmnx.my.ui.viewmodel;


import androidx.annotation.NonNull;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableList;

import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.whmnx.base.bean.OrderListBean;
import com.whmnx.base.constants.Constants;
import com.whmnx.base.constants.EventConstants;
import com.whmnx.base.http.CallBack;
import com.whmnx.base.http.HttpClient;
import com.whmnx.my.BR;
import com.whmnx.my.R;
import com.whmnx.my.databinding.ItemViewpagerBinding;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import me.goldze.mvvmhabit.base.ItemViewModel;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.bus.RxBus;
import me.goldze.mvvmhabit.bus.RxSubscriptions;
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent;
import me.goldze.mvvmhabit.utils.SPUtils;
import me.tatarka.bindingcollectionadapter2.ItemBinding;
/**
 *
 * @author goldze
 * @date 2018/7/18
 */

public class ViewPagerItemViewModel extends ItemViewModel<OrderModel> {
    public int index;
    public OrderModel viewModel;

    private int page = 1;
    private int pageSize = 10;
    private ItemViewpagerBinding viewDataBinding;

    private Disposable subscribe;

    public IWXAPI mWxApi;

    //封装一个界面发生改变的观察者
    public UIChangeObservable uc = new UIChangeObservable();

    public void setBinding(ItemViewpagerBinding binding) {
        this.viewDataBinding = binding;
        viewDataBinding.refresh.setOnRefreshListener(refreshLayout -> getSelorder(true));
        viewDataBinding.refresh.setOnLoadMoreListener(refreshLayout -> getSelorder(false));
        viewDataBinding.refresh.autoRefresh();
    }

    public class UIChangeObservable {
        //下拉刷新完成
        public SingleLiveEvent finishRefreshing = new SingleLiveEvent<>();
        //上拉加载完成
        public SingleLiveEvent finishLoadmore = new SingleLiveEvent<>();
    }

    public ViewPagerItemViewModel(@NonNull OrderModel viewModel, int index) {
        super(viewModel);
        this.index = index;
        this.viewModel = viewModel;

        mWxApi = WXAPIFactory.createWXAPI(viewModel.mFragment.getActivity(), Constants.WECHAT_ID, false);
        mWxApi.registerApp(Constants.WECHAT_ID);
        getSelorder(true);
        initView();

    }


    private void initView() {
        subscribe = RxBus.getDefault().toObservable(String.class)
                .subscribe(new Consumer<String>() {
                    @Override
                    public void accept(String object){
                        if (object.equals(EventConstants.REQUEST)){  //申请退货退款
                          //  Constants.mIndex = 6;
                            getSelorder(true);
                        }
                    }
                });
        RxSubscriptions.add(subscribe);
    }

    //我的订单
    public void getSelorder(final boolean isRefresh) {
        if (isRefresh) page = 1;
        Map<String, Object> params = new HashMap<>();
        if (index == 1){  //全部
            params.put("order_Status",10);
        }else if(index == 2){ //待支付
            params.put("order_Status",0);
        }else if (index == 3){ //待发货
            params.put("order_Status",1);
        }else if (index == 4){ //待收货
            params.put("order_Status",2);
        }else if (index == 5){  //待评价
            params.put("order_Status",3);
        }else if (index == 6){
            params.put("order_Status",5);
        }
        params.put("pageIndex",page);
        params.put("pageSize",pageSize);
        params.put("userInfo_Id", SPUtils.getInstance().getString(Constants.USERID));
        HttpClient.post(viewModel.mFragment.getActivity(), Constants.SELORDER, params,new CallBack<List<OrderListBean>>() {
            @Override
            public void onSuccess(List<OrderListBean> orderListBeans) {
                    if (isRefresh){
                        observableList.clear();
                        for (OrderListBean orderListBean : orderListBeans) {
                            observableList.add(new OrderItemViewModel2(viewModel,ViewPagerItemViewModel.this,orderListBean));
                        }
                        viewDataBinding.refresh.finishRefresh(true);
                    }else {
                        if (orderListBeans.size() > 0) {
                            for (OrderListBean orderListBean : orderListBeans) {
                                observableList.add(new OrderItemViewModel2(viewModel,ViewPagerItemViewModel.this,orderListBean));
                            }
                            viewDataBinding.refresh.finishLoadMore(true);
                        } else {
                            viewDataBinding.refresh.finishLoadMoreWithNoMoreData();
                        }
                    }
                    page++;
                }
        });
    }

    //给RecyclerView添加ObservableList
    public ObservableList<OrderItemViewModel2> observableList = new ObservableArrayList<>();
    //给RecyclerView添加ItemBinding
    public ItemBinding<OrderItemViewModel2> itemBinding = ItemBinding.of(BR.viewModel, R.layout.grid_order3);






    //下拉刷新
    public BindingCommand onRefreshCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            getSelorder(true);
        }
    });

    //上拉刷新
    public BindingCommand onLoadMoreCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            getSelorder(true);
        }
    });
}
